package com.gtw.modules.api.controller;

import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.R;
import com.gtw.dto.TbRaidDetailsDto;
import com.gtw.modules.api.service.*;
import com.gtw.modules.entity.*;
import com.gtw.modules.sys.service.SysTbClearingDetailsService;
import com.gtw.modules.sys.service.TbRaidDividendDetailsService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 会员佣金分红
 * Created by wuye on 2018/3/21.
 */
@RestController
@RequestMapping("/api/member")
public class TbMemberController {

    @Autowired
    private TbRaidDividendDetailsService tbRaidDividendDetailsService;

    @Autowired
    private TbRaidDetailsService tbRaidDetailsService;

    @Autowired
    private TbOrderService tbOrderService;

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private TbCommodityService tbCommodityService;

    @Autowired
    private TbShoppingCartServices tbShoppingCartServices;

    @Autowired
    private SysTbClearingDetailsService sysTbClearingDetailsService;

    /**
     * 获取会员佣金明细
     *
     * @return
     */
    @RequestMapping("/foundBycommission")
    public R foundBycommission(@LoginUserApi TbUser tbUser) {
        List<TbRaidDetails> tbRaidDetails = tbRaidDetailsService.queryBybenefit(tbUser.getUserId());
        return R.ok().put("data", tbRaidDetails);
    }

    /**
     * 获取会员分红明细
     *
     * @return
     */
    @RequestMapping("/foundByDividend")
    public R foundByDividend(@LoginUserApi TbUser tbUser) {
        List<TbRaidDividendDetails> tbRaidDividendDetails = tbRaidDividendDetailsService.queryBybenefit(tbUser.getUserId());
        return R.ok().put("data", tbRaidDividendDetails);
    }


    /**
     * 加载收益首页详情
     *
     * @return
     */
    @RequestMapping("/foundAll")
    public R foundAll(@LoginUserApi TbUser tbUser) {
        //总资产
        Integer totale = 0;
        totale = (tbUser.getBalance() + tbUser.getMemberbalance() + tbUser.getMemberdividend());
        //我的佣金
        Integer balance = tbUser.getMemberbalance();
        //我的分红
        Integer dividend = tbUser.getMemberdividend();
        memberDto dto = new memberDto();
        dto.setBalance(balance);
        dto.setDividend(dividend);
        /*
            判断用户身份，会员只能对普通抽佣和分红进行提现
            代理可提现所有金额
             */

        if (tbUser.getType() == 1) {
            //会员
            dto.setTixian(tbUser.getMemberbalance()+tbUser.getMemberdividend()+tbUser.getBalance());
            //已结算
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("userid",tbUser.getUserId());
            map.put("state",1);
            List<TbClearingDetails> list = sysTbClearingDetailsService.queryByUserIdAndState(map);
            Float money = 0f;
            for (TbClearingDetails tcd:list) {
                money+=tcd.getMoney();
            }
            dto.setYijiesuan((int) (money+0));
            //退款
            Map<String,Object> map2 = new HashMap<String,Object>();
            map2.put("userid",tbUser.getUserId());
            map2.put("state",2);
            List<TbClearingDetails> list1 = sysTbClearingDetailsService.queryByUserIdAndState(map);
            Float money2 = 0f;
            for (TbClearingDetails tcd:list) {
                money2+=tcd.getMoney();
            }
            dto.setTuikuan((int) (money2+0));

        } else if (tbUser.getType() == 2) {
            //代理
            dto.setTixian(tbUser.getProxybalance()+tbUser.getProxydividend()+tbUser.getMemberbalance()+tbUser.getMemberdividend()+tbUser.getBalance());
            //已结算
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("userid",tbUser.getUserId());
            map.put("state",1);
            List<TbClearingDetails> list = sysTbClearingDetailsService.queryByUserIdAndState(map);
            Float money = 0f;
            for (TbClearingDetails tcd:list) {
                money+=tcd.getMoney();
            }
            dto.setYijiesuan((int) (money+0));
            //退款
            Map<String,Object> map2 = new HashMap<String,Object>();
            map2.put("userid",tbUser.getUserId());
            map2.put("state",2);
            List<TbClearingDetails> list1 = sysTbClearingDetailsService.queryByUserIdAndState(map);
            Float money2 = 0f;
            for (TbClearingDetails tcd:list) {
                money2+=tcd.getMoney();
            }
            dto.setTuikuan((int) (money2+0));

        } return R.ok().put("data", dto);
    }


    /**
     * 优惠返佣
     *
     * @return
     */
    @RequestMapping("/foundByRebate")
    public R foundByRebate(@LoginUserApi TbUser tbUser) {
        List<TbRaidDetails> list = tbRaidDetailsService.queryBybenefit(tbUser.getUserId());
        List<TbRaidDetails> list2 = new ArrayList<>();
        for (TbRaidDetails trd : list) {
            TbOrder order = tbOrderService.queryObject(trd.getOrderid());
            if (order.getChannel().equals("1")) {
                list2.add(trd);
            }
        }
        //拼装数据
        List<TbRaidDetailsDto> dtoList = new ArrayList<>();
        for (TbRaidDetails t : list2) {
            TbRaidDetailsDto dto = new TbRaidDetailsDto();
            dto.setCreatetime(new Date());
            dto.setMoney(t.getAmount());
            TbOrder tbOrder = tbOrderService.queryObject(t.getOrderid());
            dto.setResource(tbOrder.getChannel());
            //search shoppingList
            JSONArray ja = JSONArray.fromObject(tbOrder.getShoppingcartid());
            List<TbShoppingCart> tbShoppingCartList = new ArrayList<>();
            for (int j = 0; j < ja.size(); j++) {
                TbShoppingCart tbShoppingCart = tbShoppingCartServices.selectByPrimaryKey(Integer.parseInt(ja.get(j).toString()));
                tbShoppingCartList.add(tbShoppingCart);
            }
            //search  commoditylist
            List<TbCommodity> tbCommodityList = new ArrayList<>();
            for(int i=0;i<tbShoppingCartList.size();i++){
                tbCommodityList.add(tbCommodityService.queryObject(tbShoppingCartList.get(i).getCommodityid()));
            }
            dto.setTbCommodityList(tbCommodityList);
            dto.setTargetUser(tbUserService.queryById(t.getTriggerUserid()));
            dtoList.add(dto);
        }

        return R.ok().put("data", dtoList);
    }

    /**
     * 推广收益
     */
    @RequestMapping("/foundbyPromotion")
    public R foundbyPromotion(@LoginUserApi TbUser tbUser) {
        List<TbRaidDetails> list = tbRaidDetailsService.queryBybenefit(tbUser.getUserId());
        List<TbRaidDetails> list2 = new ArrayList<>();
        for (TbRaidDetails trd : list) {
            TbOrder order = tbOrderService.queryObject(trd.getOrderid());
            if (!order.getChannel().equals(1)) {
                list2.add(trd);
            }
        }
        //拼装数据
        List<TbRaidDetailsDto> dtoList = new ArrayList<>();
        for (TbRaidDetails t : list2) {
            TbRaidDetailsDto dto = new TbRaidDetailsDto();
            dto.setCreatetime(t.getCreatetime());
            dto.setMoney(t.getAmount());
            TbOrder tbOrder = tbOrderService.queryObject(t.getOrderid());
            dto.setResource(tbOrder.getChannel());
            JSONArray ja = JSONArray.fromObject(tbOrder.getShoppingcartid());
            List<TbShoppingCart> shoppingCartList = new ArrayList<>();
            for (int i =0 ;i<ja.size();i++){
                TbShoppingCart tbShoppingCart = tbShoppingCartServices.selectByPrimaryKey(Integer.parseInt(ja.get(i).toString()));
                shoppingCartList.add(tbShoppingCart);
            }

            List<TbCommodity> commodityList = new ArrayList<>();
            for (TbShoppingCart tsc:shoppingCartList) {
                TbCommodity tbCommodity = tbCommodityService.queryObject(tsc.getCommodityid());
                commodityList.add(tbCommodity);
            }
            dto.setTbCommodityList(commodityList);
            dto.setTargetUser(tbUserService.queryById(t.getTriggerUserid()));
            dtoList.add(dto);
        }

        return R.ok().put("data", dtoList);
    }



    /**
     * 已结算
     */
    @RequestMapping("/foundAccount")
    public R foundAccount(@LoginUserApi TbUser tbUser) {

        return R.ok();
    }


    /**
     * 已退款
     */
    @RequestMapping("/foundRefund")
    public R foundRefund(@LoginUserApi TbUser tbUser) {

        return R.ok();
    }

    /**
     * 可提现
     */
    @RequestMapping("/foundWithdraw")
    public R foundWithdraw(@LoginUserApi TbUser tbUser) {
        Integer totale = 0;
        totale = 0;
        return R.ok().put("data", totale);
    }

    class memberDto {
        private Integer balance;

        private Integer dividend;

        private Integer yijiesuan;

        private Integer tuikuan;

        private Integer tixian;

        public Integer getBalance() {
            return balance;
        }

        public void setBalance(Integer balance) {
            this.balance = balance;
        }

        public Integer getDividend() {
            return dividend;
        }

        public void setDividend(Integer dividend) {
            this.dividend = dividend;
        }

        public Integer getYijiesuan() {
            return yijiesuan;
        }

        public void setYijiesuan(Integer yijiesuan) {
            this.yijiesuan = yijiesuan;
        }

        public Integer getTuikuan() {
            return tuikuan;
        }

        public void setTuikuan(Integer tuikuan) {
            this.tuikuan = tuikuan;
        }

        public Integer getTixian() {
            return tixian;
        }

        public void setTixian(Integer tixian) {
            this.tixian = tixian;
        }
    }
}
